package com.nms.se;

import java.io.IOException;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Queue;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import com.nms.se.regulation.Regulation;

public class SearchEngine {

	private Queue<Regulation> reserveQueue;
	private Queue<Regulation> visitedQueue;

	public SearchEngine() {
		reserveQueue = new LinkedList<Regulation>();
		visitedQueue = new LinkedList<Regulation>();
	}

	/**
	 * 启动搜索
	 */
	public void start() {
		Regulation r;
		while (!reserveQueue.isEmpty()) {
			r = reserveQueue.remove();
			if (visitedQueue.contains(r))
				continue;
			System.out.println("Link Regex : " + r.getLinkRegex());
			try {
				Searcher st = new Searcher(r);
				System.out.println("Content Regex : " + r.getContentRegex());
				st.start();
			} catch (Exception e) {
				reserveQueue.add(r);
				continue;
			}
			visitedQueue.add(r);
		}
	}

	// /////////////////////////////
	public void add(Regulation r) {
		reserveQueue.add(r);
	}

	public void addAll(Collection<Regulation> rs) {
		reserveQueue.addAll(rs);
	}

	public void remove(Regulation r) {
		reserveQueue.remove(r);
	}

	public void removeAll() {
		reserveQueue.clear();
	}

	public boolean isEmpty() {
		return reserveQueue.isEmpty();
	}

	// /////////////////////////////////////

	public Queue<Regulation> getReserveQueue() {
		return reserveQueue;
	}

	public void setReserveQueue(Queue<Regulation> reserveQueue) {
		this.reserveQueue = reserveQueue;
	}

	public Queue<Regulation> getVisitedQueue() {
		return visitedQueue;
	}

	public void setVisitedQueue(Queue<Regulation> visitedQueue) {
		this.visitedQueue = visitedQueue;
	}

	/**
	 * 
	 * @author Administrator
	 * 
	 */
	public class Searcher {

		private Queue<String> queue;
		private Regulation regulation;

		public Searcher(Regulation r) {
			this.setRegulation(r);
			queue = new LinkedList<String>();
		}

		public void start() {
			HttpClient hc = new DefaultHttpClient();
			HttpGet hg = new HttpGet(regulation.getUrl());

			String content = null;
			HttpResponse hr;
			try {
				hr = hc.execute(hg);
				HttpEntity entity = hr.getEntity();

				if (entity != null) {
					content = EntityUtils.toString(entity);
					String regex = Regulation.a;
					Pattern p = Pattern.compile(regex);
					Matcher m = p.matcher(content);
					Pattern hrefPattern = Pattern.compile(regulation
							.getLinkRegex());
					String href = "";
					while (m.find()) {
						href = content.substring(m.start(), m.end());
						Matcher hm = hrefPattern.matcher(href);
						if (hm.find()) {
							String substring = href.substring(hm.start(),
									hm.end());
							int s = substring.indexOf("\"");
							int e = substring.lastIndexOf("\"");
							if (s < 0)
								s = substring.indexOf("\'");
							if (e < 0)
								e = substring.lastIndexOf("\'");
							if (substring.substring(s + 1, e).startsWith(
									"http:")) {
								queue.add(substring.substring(s + 1, e));
							}
						}
					}
					int i = 0;
					/** 抓取新闻内容部分 */
					while (!queue.isEmpty()) {
						String u = queue.remove();
						System.out.println((i++) + " : " + u);
						
						hr = hc.execute(new HttpGet(u));
						HttpEntity he = hr.getEntity();
						if(he != null){
							String c = EntityUtils.toString(he);
						}
					}
				}
			} catch (ClientProtocolException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				hc.getConnectionManager().shutdown();
			}
		}

		public Regulation getRegulation() {
			return regulation;
		}

		public void setRegulation(Regulation regulation) {
			this.regulation = regulation;
		}
	}
}
